package y24.m1.homework;
public class Test4 {
    public static void main(String[] args) {
        double a = 1;//积分的下限
        double b = 5;//积分的上限
        double integral = fun(a, b);
        System.out.println("函数x^3 + x在积分区间[" + a + ", " + b + "]上的定积分为：" + integral);
    }
    public static double fun(double a, double b) {
        double h = 0.0001; // 步长h的值
        int n = (int) ((b - a) / h); // 区间个数

        double sum = 0.0;
        for (int i = 0; i < n; i++) {
            double x = a + i * h; // 计算每个小区间的起始值
            double f = x * x * x + x; // 计算函数值
            sum += f;
        }
        double integral = h * sum; // 计算定积分的近似值
        return integral;
    }
}
